import {
    MODEL_LIST,
    GET_SHOP_INFO,
    SET_USER_TOKEN,
    SHOW_LOADING,
    HIDE_LOADING,
    GET_TOKEN,
    GET_SHOP_DATA,
    CLEAR_TOKEN,
    CHANGE_REFRESH_STATE,
    SHOP_CHANGE_STATE,
    GOTO_ADDPRODUCT,
    GOTO_CERT
} from './constants';

import {
    Alert
} from 'react-native';

import NetUtil from '../../common/NetUtil';
import config from '../../config/config';

import {TokenManager, LoginOutPlugin, NaviManager} from '../../utils/NativeUtil';
import {iosAllModels, androidAllModels} from './reducer';
import NaviUtil from '../../utils/NaviUtil';

let allModels = [];

var Platform = require('Platform');
if (Platform.OS === 'android') {
    allModels = androidAllModels
} else {
    allModels = iosAllModels
}

import {getConfig, configkey} from '../../utils/NativeUtil'

const ShopInfoAction = (shop) => ({
    type: GET_SHOP_INFO,
    payload: {
        shopInfo: {
            shopname: shop.shopname,
            qrcode: shop.qrcode,
            shoplogo: shop.shoplogo,
            shopFlag: shop.shopFlag,
            brandSet: shop.brandSet,
            notice: shop.notice,
            shopid: shop.shopid,
            story: shop.story == undefined ? "" : shop.story,
        }
    }
});

export const changeShopInfo = (shop) => {
    return dispatch => {
        dispatch(ShopInfoAction(shop));
    }
}

export const gotoCert = (value) => {
    return dispatch => {
        dispatch({
            type:GOTO_CERT,
            payload:{
                is_from_account:value
            }
        })
    }
} 

export const changeShopState = (state) => {
    return dispatch => {
        dispatch({
            type: SHOP_CHANGE_STATE,
            payload: {
                shopState: state
            }
        })
    }
}

export const getShopInfo = () => {
    return dispatch => {
        dispatch(changeRefresh(true));

        let keys = `${configkey.ShopToken},${configkey.UserToken}`;
        getConfig(keys, (values) => {
            let token = values[configkey.ShopToken];
            let userToken = values[configkey.UserToken];

            if (userToken != undefined) {
                dispatch(setUserToken(userToken));
            }

            if (token == "") {
                dispatch(changeShopState(0));
                dispatch(changeRefresh(false));
            } else {
                const params = {
                    token: token,
                };
                dispatch(getToken(token));
                let url = "/s/shop/detail";

                NetUtil.postSign(config.apiUrl + url, params, function (res) {
                    if (res.success) {
                        let data = res.data;
                        let logo = "";

                        if (data != undefined) {
                            if (data.logo != undefined) {
                                logo = data.logo;
                            }

                            if (logo != undefined) {
                                if (logo.indexOf("https") > 0 || logo.indexOf("https") == 0) {
                                    logo = logo;
                                } else {
                                    logo = logo.replace(/http/, "https");
                                }
                            }

                            console.log(data);

                            const shop = {
                                shopname: data.shopName,
                                shoplogo: logo,
                                shopFlag: data.shopFlag,
                                brandSet: data.brandSet,
                                story: data.story,
                                notice: data.notice,
                                shopid: data.shopId,
                            };

                            if (data.status == 1) {
                                dispatch(changeShopState(2));
                                dispatch(ShopInfoAction(shop));
                                dispatch(getShopData());
                            } else if (data.status == 2) {
                                dispatch(changeShopState(3));
                                dispatch(changeRefresh(false));
                            } else if (data.status == -2) {
                                dispatch(changeShopState(1));
                                dispatch(changeRefresh(false));
                            }
                        } else {
                            LoginOutPlugin.loginOutWithOutAlert();
                        }
                    }
                });
            }
        });
    }
};

export const showLoading = () => {
    return dispatch => {
        dispatch({
            type: SHOW_LOADING,
            payload: {isloading: true}
        })
    }
}

export const hideloading = () => {
    return dispatch => {
        dispatch({
            type: HIDE_LOADING,
            payload: {isloading: false}
        })
    }
}

export const getToken = (token, usertoken) => {

    let tokens = {};
    if (token) {
        tokens.token = token;
    }
    if (usertoken) {
        tokens.userToken = usertoken;
    }

    return dispatch => {
        dispatch({
            type: HIDE_LOADING,
            payload: tokens
        })
    }
};

export const clearToken = () => {
    return dispatch => {
        dispatch({
            type: CLEAR_TOKEN,
            payload: {
                token: "",
                userToken: "",
            }
        })
    }
}

export const setShopToken = () => {

};

export const setUserToken = (token) => {
    return dispatch => {
        dispatch({
            type: SET_USER_TOKEN,
            payload: {userToken: token}
        })
    }
};

export const showQrImg = () => {
    return dispatch => {
        dispatch({
            type: SHOW_LOADING,
            payload: {shopImgModalVisible: true}
        })
    }
}

export const hideQrImg = () => {
    return dispatch => {
        dispatch({
            type: HIDE_LOADING,
            payload: {shopImgModalVisible: false}
        })
    }
}

const ModelListAction = (list) => {
    if (list == undefined) {
        return {
            type: MODEL_LIST,
            payload: {
                models: []
            }
        }
    }

    let listArr = list.split(",");
    var visiableModels = new Array();
    for (i in allModels) {
        let iscontain = false;
        for (j in listArr) {
            if (listArr[j] == allModels[i].tag) {
                iscontain = true;
                allModels[i].canUse = true;
                visiableModels[visiableModels.length] = allModels[i];
            }
        }

        if (!iscontain) {
            allModels[i].canUse = false;
            visiableModels[visiableModels.length] = allModels[i];
        }
    }

    // return {
    //     type:MODEL_LIST,
    //     payload:{
    //         models:visiableModels
    //     }
    // }
}

export const showModels = () => {
    return dispatch => {
        let keys = `${configkey.ShopToken}`;
        getConfig(keys, (values) => {
            let token = values[configkey.ShopToken];
            let shopId = values[configkey.ShopId];
            const params = {
                token: token,
                shopId: shopId,
                key: 'modals',
            };
            let url = '/s/shopvip/rightConfig';
            NetUtil.postSign(config.apiUrl + url, params, function (res) {
                let data = res.data;
                dispatch(ModelListAction(data));
            });
        });
    }
}

export const getShopData = () => {
    return dispatch => {

        let keys = `${configkey.ShopToken},${configkey.UserToken}`;
        getConfig(keys, (values) => {
            let shoptoken = values[configkey.ShopToken];
            let usertoken = values[configkey.UserToken];
            const params = {
                token: shoptoken,
                usertoken: usertoken
            };
            dispatch(getToken(shoptoken, usertoken));
            let url = "/s/shop/data";
            NetUtil.postSign(config.apiUrl + url, params, function (res) {
                dispatch(changeRefresh(false));
                if (res.success) {
                    let data = res.data;
                    dispatch({
                        type: GET_SHOP_DATA,
                        payload: {
                            shopData: data,
                        }
                    });
                } else {
                    LoginOutPlugin.loginOutWithOutAlert();
                }
            });
        });
    }
};

export const changeRefresh = (refreshLoading) => {
    return dispatch => {
        dispatch({
            type: CHANGE_REFRESH_STATE,
            payload: {
                refreshLoading: refreshLoading
            }
        })
    }
}

export const gotoAddProduct = (value) => {
    return dispatch => {
        dispatch({
            type: GOTO_ADDPRODUCT,
            payload: {
                is_from_home: value
            }
        })
    }
}